<?php

namespace App\Http\Controllers\Api;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Transformers\CategoryTransformer;

class CategoriesController extends Controller
{
    public function index()
    {
        return $this->response->collection(Category::all(), new CategoryTransformer());
    }

    public function getBtc(Request $request)
    {
        $page = $request->page ?? 1;
        if($page > 3 || !is_numeric($page)) {
            return $this->response->error('数据不正确',422);
        }
        $file_info = file_get_contents(public_path("btc_dir/btc_lists".$page.".json"));
        $file_json = json_decode($file_info,true);
        foreach($file_json as &$v) {
            $v['rank'] = $v['rank'];
            $v['companyAbbreviation'] = $v['desc'];
            $v['companyIcon;'] = $v['coin_logo'];
            $v['companyName'] = $v['name'];
            $v['latestPrice'] =  trim(str_replace("\n","",$v['price']));
            $v['decline'] =  trim(str_replace("\n","",$v['up']));
            $v['bubTotalTurnover'] =  trim(str_replace("\n","",$v['change']));
            $v['marketValue'] =  trim(str_replace("\n","",$v['total']));
            $v['linkUrl'] = trim($v['href']);
            unset($v['href']);
            unset($v['coin_logo']);
            unset($v['name']);
            unset($v['desc']);
            unset($v['price']);
            unset($v['up']);
            unset($v['change']);
            unset($v['total']);
        }
        return $this->response->array($file_json);
    }
}
